<?php

namespace app\api\controller;


use app\api\model\project\ProjectApply as ProjectApplyModel;
use app\api\model\project\ProjectEvaluateComment;
use app\api\model\project\ProjectEvaluateZan;
use app\common\controller\Api;
use app\api\model\project\ProjectEvaluate as ProjectEvaluateModel;

class Projectevaluate extends Api
{
    protected $noNeedRight = ['*'];
    protected $noNeedLogin = ['lists'];

    //点评排名
    public function ranking()
    {
        $city_id = input('city_id',0);
        $where = ['pa.status'=>1];
        /*if ($city_id){
            $where['ps.city_id'] = $city_id;
        }*/

        $project = ProjectApplyModel::alias('pa')
            ->join('project_staff ps','pa.staff_id = ps.id')
            ->where($where)
            ->field('pa.staff_id,pa.is_recommend,pa.price,pa.unit,pa.name,ps.image,pa.status,ps.nickname,ps.realname,ps.title,
            ps.comment_num')
            ->order('ps.comment_num desc')
            ->paginate(10, false, ['query' => request()->get()]);
        $this->success('',$project);
    }

    //我的点评
    public function myEvaluate()
    {
        $page_size = input('page_size',10);

        $where = ['ge.user_id'=>$this->auth->id];

        $list = ProjectEvaluateModel::alias('ge')
            ->join('project_staff ps','ge.staff_id = ps.id')
            ->join('project_apply pa','ge.apply_id = pa.id')
            ->join('user u','ge.user_id = u.id')
            ->where($where)
            ->field('ge.*,u.nickname,u.avatar,ps.image,pa.status,ps.nickname,ps.realname,ps.title,pa.staff_id,pa.is_recommend,pa.price,pa.unit')
            ->order('ge.createtime desc')
            ->paginate($page_size, false, ['query' => request()->get()]);
        foreach ($list as $k=>$v){
            $info = ProjectEvaluateZan::where(['evaluate_id'=>$v['evaluate_id'], 'user_id' => $this->auth->id])->find();
            $list[$k]['is_zan'] = $info?1:0;
            $list[$k]['comment_num'] = ProjectEvaluateComment::where(['evaluate_id'=>$v['evaluate_id']])->count();
        }
        $this->success('',$list);
    }

    //评价列表
    public function lists()
    {
        $page_size = input('page_size',10);
        $staff_id = input('staff_id',0);

        $where = ['ge.staff_id'=>$staff_id];

        $list = ProjectEvaluateModel::alias('ge')
            ->join('user u','ge.user_id = u.id')
            ->where($where)
            ->field('ge.*,u.nickname,u.avatar')
            ->order('ge.createtime desc')
            ->paginate($page_size, false, ['query' => request()->get()]);
        foreach ($list as $k=>$v){
            if ($this->auth->id){
                $info = ProjectEvaluateZan::where(['evaluate_id'=>$v['evaluate_id'], 'user_id' => $this->auth->id])->find();
                $list[$k]['is_zan'] = $info?1:0;
            }

            $list[$k]['comment_num'] = ProjectEvaluateComment::where(['evaluate_id'=>$v['evaluate_id']])->count();
        }
        $this->success('',$list);
    }

    //提交评价
    public function add()
    {
        try {
            $model = new ProjectEvaluateModel();
            $param = input();
            $param['user_id'] = $this->auth->id;
            $res = $model->add($param);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('提交成功',$res);

    }

    //评论评价
    public function comment()
    {
        $comment_model = new ProjectEvaluateComment();

        $data = [
            'user_id'=>$this->auth->id,
            'evaluate_id'=>input('evaluate_id',0),
            'content'=>input('content',''),
        ];

        $info = \app\api\model\project\ProjectEvaluate::get($data['evaluate_id']);

        if (empty($info))$this->error('评价不存在');

        if (empty($data['evaluate_id']))$this->error('缺少参数评价id');

        if (empty($data['content']))$this->error('请输入评论内容');

        $res = $comment_model->save($data);

        $this->success('评论成功');
    }

    //评论列表
    public function commentlists()
    {
        $page_size = input('page_size',10);
        $evaluate_id = input('evaluate_id',0);

        $where = ['ge.user_id'=>$this->auth->id,'ge.evaluate_id'=>$evaluate_id];

        $list = ProjectEvaluateComment::alias('ge')
            ->join('user u','ge.user_id = u.id')
            ->where($where)
            ->field('ge.*,u.nickname,u.avatar')
            ->order('ge.createtime desc')
            ->paginate($page_size, false, ['query' => request()->get()]);

        $this->success('',$list);

    }
}